package com.ve.searcher.lucutil


import org.apache.lucene.document.Document
import org.apache.lucene.document.Field
import org.apache.lucene.document.StringField
import org.apache.lucene.document.TextField
import org.apache.lucene.index.IndexWriter

import java.io.File
import java.nio.file.Files

//import org.springframework.web.multipart.MultipartFile;

object LuceneIndex {
    @Throws(Exception::class)
    fun indexDocument(
            file: File, tags: Array<String>
            //            ,ArrayList<String> groupCodes,
            //            String fileType,
            //            String viewType,
            //            String body
    )//            ,String id,
    //            String owner,
    //            String time,
    //            String date
    {
        val indexWriter = LuceneInit.indexWriter

        // new Document
        val document = Document()

        // add id
        //        document.add(new StringField("id", id, Field.Store.YES));

        // add path
        document.add(StringField("path", file.absolutePath, Field.Store.YES))

        // add file
        document.add(StringField("file", file.name, Field.Store.YES))

        // add owner
        //        document.add(new StringField("owner", owner, Field.Store.YES));

        // add timestamp

        //        document.add(new StringField("createDate", time, Field.Store.YES));

        // add date
        document.add(StringField("lastModified", file.lastModified().toString(), Field.Store.YES))

        // add tags
        for (tag in tags) {
            document.add(StringField("tags", tag, Field.Store.YES))
        }

        // add groupCodes
        //        for (String groupCode : groupCodes) {
        //            document.add(new StringField("groupCodes", groupCode, Field.Store.YES));
        //        }

        // add body
        document.add(TextField("body", Files.readAllLines(file.toPath()).joinToString(""), Field.Store.YES))

        // add fileType
        //        document.add(new StringField("fileType", fileType.split(";")[0], Field.Store.YES));

        // add viewType
        //        document.add(new StringField("viewType", viewType, Field.Store.YES));

        // add the document and commit
        indexWriter!!.addDocument(document)
        indexWriter!!.commit()
    }
}
